package com.hmdp.utils;

import javafx.scene.input.DataFormat;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

@Component
public class RedisWorker {
    @Resource
    StringRedisTemplate stringRedisTemplate;

    private long BEGIN_TIME = 1685577600L;//2023/6/1/0.0.0

    /**
     * 使用时间戳+序列号
     */
    public long getUnitId(String key){
        //获取时间戳
        long currentTime = LocalDateTime.now().toEpochSecond(ZoneOffset.UTC);
        long time = currentTime-BEGIN_TIME;
        //获取序列号
        LocalDate now = LocalDate.now();
        String format = now.format(DateTimeFormatter.ofPattern("yyyy:MM:dd"));
        Long serialNum = stringRedisTemplate.opsForValue().increment("inc:" + key + format);
        //拼接
        return time << 32 | serialNum;
    }
}
